package com.dateview.test.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.dateview.test.R;
import com.dateview.test.view.MyGridView;
import com.dateview.test.view.OnChildClickListener;

import java.util.List;

/**
 * Author: dengdayi
 * Date:2016/1/11 16:04
 * Description:
 */
public class ExpandableAdapter extends BaseExpandableListAdapter {

    private Context mContext;

    private SparseArray<List<String>> mArray;

    private LayoutInflater mLayoutInflater;

    private OnChildClickListener mChildClickListener;

    public ExpandableAdapter(Context context, SparseArray<List<String>> mArray) {
        this.mContext = context;
        this.mArray = mArray;
        if (mLayoutInflater == null) {
            mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    }

    @Override
    public int getGroupCount() {
        return mArray.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mArray.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mArray.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewGroupHolder mGroupHolder = null;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.group_list_item, null);
            mGroupHolder = new ViewGroupHolder();
            mGroupHolder.mTextView = (TextView) convertView.findViewById(R.id.tv_text);
            convertView.setTag(mGroupHolder);
        } else {
            mGroupHolder = (ViewGroupHolder) convertView.getTag();
        }
        //设置数据
        String month = String.valueOf(mArray.keyAt(groupPosition) + 1);
        mGroupHolder.mTextView.setText("2016年" + month + "月");
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewChildHolder mChildHolder = null;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.child_list_item, null);
            mChildHolder = new ViewChildHolder();
            mChildHolder.myGridView = (MyGridView) convertView.findViewById(R.id.mgv_days_item);
            convertView.setTag(mChildHolder);
        } else {
            mChildHolder = (ViewChildHolder) convertView.getTag();
        }
        //设置数据
        List<String> mStrings = mArray.get(groupPosition);
        if (mStrings != null) {
            mChildHolder.myGridView.setAdapter(new GirdDateAdapter(mContext, mStrings));

            mChildHolder.myGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    if (mChildClickListener != null) {
                        mChildClickListener.onChildClick(parent, view, groupPosition, position, id);
                    }
                }
            });
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        //
        return true;
    }

    public void setOnChildClickListener(OnChildClickListener mChildClickListener) {
        this.mChildClickListener = mChildClickListener;
    }

    private class ViewGroupHolder {
        private TextView mTextView;
    }

    private class ViewChildHolder {
        private MyGridView myGridView;
    }


}
